var SystemConfigJs = function (targetObj) {
    this.targetObj = targetObj;
    this.sysUtils = new kendoUtils();
    this.init();
    return this;
}
SystemConfigJs.prototype = {
    init: function () {
        var fieldArray = this.sysUtils.getBindFieldArray(this.targetObj);
        var functionArray = [
            {fName: 'mqConfigSave', fs: this.sysConfigBtn},
            {fName: "SMSConfigSave", fs: this.sysConfigBtn},
            {fName: "sweepConfigSave", fs: this.sysConfigBtn},
            {fName: "workConfigSave", fs: this.sysConfigBtn},
            {fName: "controlPanelSave", fs: this.sysConfigBtn},
            {fName: "assignmentSave", fs: this.sysConfigBtn},
            {fName: "dataUploadSave", fs: this.sysConfigBtn},
            {fName: "diskCleanupSave", fs: this.sysConfigBtn},
            {fName: "sendTestSMS", fs: this.sendTestSMS}
        ];
        this.models = new bindBaseModel(null, fieldArray, functionArray, null);
        this.sysUtils.bindDoucment(this.models, this.targetObj, this);
        this.targetObj.find("div[name='tabStripDiv']").css("height", this.targetObj.parent().height());
        $("#tabstrip-left").kendoTabStrip({
            tabPosition: "left",
            animation: {open: {effects: "fadeIn"}},
        });
        //初始化配置界面
        this.loadConfig();
    },
    loadConfig: function () {
        var that = this;
        JsonpAjax(baseApiPath + "PbaSystemConfig/configAll", null, function (res) {
            //遍历整个对象
            var object = null;
            for (var field in res.data) {
                object = res.data[field];
                that.sysUtils.setBindFieldValue(that.objectConvetJson(object));
                that.changeMqButton(object["opType"], field);
            }
        });
    },
    /**
     * 对象转成页面能赋值的json
     */
    objectConvetJson: function (object) {
        var paramsJson = new Object();
        for (var names in object) {
            if (typeof object[names] == "object") {
                for (var nextNames in object[names]) {
                    if (typeof object[names][nextNames] == "object") {
                        for (var lastNames in object[names][nextNames]) {
                            paramsJson[names + "_" + nextNames + "_" + lastNames] = object[names][nextNames][lastNames];
                        }
                    } else {
                        paramsJson[names + "_" + nextNames] = object[names][nextNames];
                    }
                }
            } else {
                paramsJson[names] = object[names];
            }
        }
        return paramsJson;
    },
    /**
     * 保存相关配置
     */
    sysConfigBtn: function (e) {
        var button = $(e.target);
        var that = this;
        if (!isEmpty(e)) {
            that = $(e.target).data('that');
        }
        var parnetsDiv = button.parents("div[cns]");
        if (!validityForm(parnetsDiv)) return;
        var bindFieldArray = that.sysUtils.getBindFieldArray(parnetsDiv);
        var json = that.sysUtils.getBindDocJson(bindFieldArray);
        json = that.jsonConvertObject(json);
        json["opType"] = button.attr("state");
        json["serviceCode"] = parnetsDiv.attr("code");
        json["taskTypes"] = parnetsDiv.parents("div[taskTypes]").attr("taskTypes");
        JsonpAjax(baseApiPath + "systemConfig/" + parnetsDiv.attr("cns"), JSON.stringify(json), 'json', function (res) {
            that.changeMqButton(json["opType"], parnetsDiv.attr("code"));
            if (null != res && typeof res == "object") {
                if (true == res["isSucess"]) {   //连接成功，那么只允许修改参数，不允许修改ip和端口、编码格式
                    that.targetObj.find("input[data-bind='value:receiveIP']").attr("disabled", "disabled");
                    that.targetObj.find("input[data-bind='value:receivePort']").attr("disabled", "disabled");
                    that.targetObj.find("input[data-bind='value:jnaEncoding']").attr("disabled", "disabled");
                }
            }
        });
    },
    /**
     * 把json转换为后台能用的对象
     * @param paramsJson  参数json
     */
    jsonConvertObject: function (paramsJson) {
        var object = new Object();
        var fields = null;
        var fieldsVla = null;
        var reg = new RegExp("^[0-9]*$");
        for (var names in paramsJson) {
            if (isEmpty(paramsJson[names])) continue;
            fieldsVla = reg.test(paramsJson[names]) && paramsJson[names].length <= 17 ? parseInt(paramsJson[names]) : paramsJson[names];
            field = names.split("_");
            if (names.indexOf("_") > 0) {
                if (field.length == 2) {		//一层
                    object[field[0]] = null == object[field[0]] ? new Object() : object[field[0]];
                    object[field[0]][field[1]] = fieldsVla;
                } else {
                    object[field[0]] = null == object[field[0]] ? new Object() : object[field[0]];
                    object[field[0]][field[1]] = null == object[field[0]][field[1]] ? new Object() : object[field[0]][field[1]];
                    object[field[0]][field[1]][field[2]] = fieldsVla;
                }
                continue;
            }
            object[names] = fieldsVla;
        }
        return object;
    },
    /**
     * 更改mq按钮
     * @param opType
     */
    changeMqButton: function (opType, code) {
        var div = this.targetObj.find("div[code='" + code + "']");
        var button = div.find("button:last");
        //根据成功或者失败的结果，更新相应的配置
        if (opType == "Start") {   //启动服务
            button.attr("state", "Stop");
            button.text("编辑配置信息并停止服务");
            div.find("input").attr("disabled", "disabled");
//				this.targetObj.find("#excepMsg").remove();
        } else {
            div.find("input").removeAttr("disabled");
            button.text("保存配置信息并启动服务");
            button.attr("state", "Start");
        }
        if (code == "SMS" && opType == "Start") {
            div.find("#testMobile").removeAttr("disabled");
            div.find("#testMobile").parents("#testDiv").show();
        } else if (code == "SMS" && opType == "Stop") {
            div.find("#testMobile").parents("#testDiv").hide();
        }
    },
    /**
     * 发送测试短信
     * @param e
     */
    sendTestSMS: function (e) {
        var that = $(e.target).data('that');
        var testMobile = that.targetObj.find("#testMobile").val();
        var reg = new RegExp("^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$");
        if (!reg.test(testMobile)) {
            notification({title: '提示', message: '请填写正确的手机号码！！'}, 'info');
            return;
        }
        var json = {"testMobile": testMobile};
        JsonpAjax(baseApiPath + "systemConfig/sendTestSMS", JSON.stringify(json), 'json', function (res) {

        });
    }
}